package control;

import Entity.userBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class userControl {
    //获取数据库中的普通用户
    public static void getcomonUser(HttpServletRequest request, HttpServletResponse response)
    {
        try{
            Connection con=null;
            Statement stmt=null;
            ResultSet rs=null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url="jdbc:h2:~/test1";
            //System.out.println("到达");
            con= DriverManager.getConnection(url,"root","root");
            //System.out.println("到达2");
            HttpSession session=request.getSession();
            stmt=con.createStatement();
            String sql="select * from shop_user where  role = '1'";
            rs=stmt.executeQuery(sql);
            ArrayList userslist=null;
            if((ArrayList)session.getAttribute("userslist")==null){
                userslist=new ArrayList();
                while(rs.next()){
                    userBean ff=new userBean();
                    ff.setUse_id(rs.getString("use_id"));
                    ff.setUsername(rs.getString("username"));
                    ff.setAddress(rs.getString("address"));
                    ff.setDgutnumber(rs.getString("dgutnumber"));
                    ff.setImg(rs.getString("img"));
                    ff.setPassword(rs.getString("password"));
                    ff.setPhone(rs.getString("phone"));
                    ff.setRole(rs.getString("role"));
                    ff.setSex(rs.getString("sex"));
                    userslist.add(ff);
                }
                session.setAttribute("userslist", userslist);
            }
            rs.close();
            stmt.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return;
    }
    public static void changeuser(userBean ff,HttpServletRequest request, HttpServletResponse response)
    {
        try {
            Connection con = null;
            Statement stmt = null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url = "jdbc:h2:~/test1";
            //System.out.println("到达");
            con = DriverManager.getConnection(url, "root", "root");
            //System.out.println("到达2");
            HttpSession session = request.getSession();
            stmt=con.createStatement();
                //new String(fi.getString().getBytes("ISO-8859-1"),"UTF-8")
            String sql="UPDATE \"shop_user\" SET username='"+ff.getUsername()+"',ADDRESS='"+ff.getAddress()+"',sex='"+ff.getSex()+"',phone='"+ff.getPhone()+"' WHERE use_id='"+ff.getUse_id()+"'";
            stmt.executeUpdate(sql);
            System.out.println("删除成功");
            stmt.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return;
    }
}
